package com.inventory.api.controller;

import com.inventory.api.dao.StockOrderDao;
import com.inventory.api.dao.StockTransactionDao;
import com.inventory.api.signUpReq.RecordDetailsSignUpReq;
import com.inventory.api.vo.StockRecordDetailVo;
import com.inventory.api.vo.StockTransactionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2018/2/2.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/inventory", produces = "application/json;charset=utf-8")
public class StockOrderController {

    @Autowired
    StockOrderDao stockOrderDao;

    @Autowired
    StockTransactionDao stockTransactionDao;


    /**
     * 获取出入库记录
     *
     * @param storeId
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/record")
    public ResponseEntity getStockRecord(@RequestParam(value = "storeId") String storeId,
                                         @PageableDefault(value = 10, sort = {"operTime"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return new ResponseEntity(stockOrderDao.findByStoreId(storeId, pageable), HttpStatus.OK);
    }


    /**
     * 查询入库详情
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/record-details")
    public ResponseEntity getStockRecordDetails(@RequestParam(value = "id") String id) {
        try {
            return new ResponseEntity(stockTransactionDao.findByRefIdOrderByIdDesc(id).
                    stream().map(e -> new StockTransactionVo(e)).collect(Collectors.toList()), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("queryProduct is error==> id:{}", id, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    /**
     * 出入库明细查询
     *
     * @param signUpReq
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/query/record-details")
    public ResponseEntity getQueryRecordDetails(@RequestBody RecordDetailsSignUpReq signUpReq) {
        try {
            /**
             * 时间转换
             */
            long totalElements = stockTransactionDao.queryStockDetailsNumber(signUpReq);
            int totalPages = (int) Math.ceil(totalElements / (signUpReq.getSize() * 1.0));
            StockRecordDetailVo recordDetailVo = new StockRecordDetailVo(totalPages, totalElements);
            /**
             * 查询明细
             */
            List<Map<String, Object>> mapList = stockTransactionDao.queryStockDetails(signUpReq);
            recordDetailVo.setRecords(mapList);
            return new ResponseEntity(recordDetailVo, HttpStatus.OK);

        } catch (Exception ex) {
            log.error("getQueryRecordDetails is error==> signUpReq:{}", signUpReq, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
